<?php
namespace app\admin\controller;
use app\model\WxGoods as WxGoodsModel;
use think\Db;


class WxGoods extends Base
{
    public function __construct(){
        $model = new WxGoodsModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '活动';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $this->page_info['view'] = 'save';
        $model = $this->model;

        $categorys = Db::name('wx_goods_category')->select();
        $categorys = arrayParamExtract($categorys,'id','name');

        $fields = [
            'type'=>['name'=>'类型','type'=>'select1','params'=>$model->getTypeList()],
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'goods_category_id'=>['name'=>'分类','type'=>'select1','params'=>$categorys],
            'cover'=>['name'=>'封面','type'=>'file'],
            'images'=>['name'=>'轮播图，尺寸建议：750 * 1333','type'=>'files'],
            'sort'=>['name'=>'排序值','type'=>'text'],
            'description'=>['name'=>'描述','type'=>'textarea'],
            'introduce'=>['name'=>'介绍图片','type'=>'files'],
            'content'=>['name'=>'内容','type'=>'ueditor'],
            'view_num'=>['name'=>'浏览数','type'=>'text'],
            'tags'=>['name'=>'标签','type'=>'text','placeholder'=>'多个用 , 分割'],
            
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'type','name'=>'类型','type'=>'select1_1','params'=>$model->getTypeList()],
                ['key'=>'name','name'=>'名称','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
            ['key'=>'type','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['category'] = Db::name('wx_goods_category')->field('name')->where('id',$vo['goods_category_id'])->find();
            $list['data'][$k]['images'] = explode(',',$vo['images']);
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','goods_category_id','cover','type'],
                'nomust'=>['images','description','sort','content','introduce','tags','view_num','params','skus']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            if (!empty($save_data['params'])) {
                $params = [];
                foreach ($save_data['params']['name'] as $k => $vo) {
                    $params[] = [
                        'name'=>$save_data['params']['name'][$k],
                        'value'=>$save_data['params']['value'][$k],
                    ];
                }
                $save_data['params'] = json_encode($params,JSON_UNESCAPED_UNICODE);
            }
            if (!empty($save_data['skus'])) {
                $skus = [];
                foreach ($save_data['skus']['name'] as $k => $vo) {
                    $skus[] = [
                        'name'=>$save_data['skus']['name'][$k],
                        'price'=>$save_data['skus']['price'][$k],
                        'stock'=>$save_data['skus']['stock'][$k],
                    ];
                }
                $save_data['price'] = min($save_data['skus']['price']);
                $save_data['skus'] = json_encode($skus,JSON_UNESCAPED_UNICODE);
            }
        }

        return parent::saveData($save_data);
    }
            
    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','goods_category_id','cover','type'],
                'nomust'=>['images','description','sort','content','introduce','tags','view_num','params','skus']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            if (!empty($save_data['params'])) {
                $params = [];
                foreach ($save_data['params']['name'] as $k => $vo) {
                    $params[] = [
                        'name'=>$save_data['params']['name'][$k],
                        'value'=>$save_data['params']['value'][$k],
                    ];
                }
                $save_data['params'] = json_encode($params,JSON_UNESCAPED_UNICODE);
            }
            if (!empty($save_data['skus'])) {
                $skus = [];
                foreach ($save_data['skus']['name'] as $k => $vo) {
                    $skus[] = [
                        'name'=>$save_data['skus']['name'][$k],
                        'price'=>$save_data['skus']['price'][$k],
                        'stock'=>$save_data['skus']['stock'][$k],
                    ];
                }
                $save_data['price'] = min($save_data['skus']['price']);
                $save_data['skus'] = json_encode($skus,JSON_UNESCAPED_UNICODE);
            }

            $t_fields = $model->getTableFields();
            if (!isset($save_data['utime']) && in_array('utime', $t_fields)) {
                $save_data['utime'] = time();
            }

            if ( $model->where('id',$id)->update($save_data) !== false ) {
                $this->success("编辑成功",url('index'));
            }else{
                $this->error("编辑失败");
            }
        }

        $this->assignFields();
        $data = $model->where('id',$id)->find();
        if (!empty($data)) {
            $data['params'] = json_decode($data['params'],true);
            $data['skus'] = json_decode($data['skus'],true);
        }
        return view(emptyDef($this->page_info['view'],'common/save'), [
            'data'=>$data
        ]);
    }




   
}
